import React, { Component } from 'react';
import { actions } from 'mirrorx';
import { Dropdown, Menu, Button, Message,Upload } from 'tinper-bee';
import {searchField} from '../InitTemplate';
import { Warning,handleSearchValue } from 'utils';
import './index.less';

const { Item } = Menu;
class AcExport extends Component {
    constructor(props) {
        super(props);
        this.state = {  
            selIndex: "0",
        };

    }

    // 下拉选择函数
    onSelect = ({ key }) => {
        
        this.setState({
            selIndex : key
        })

        this.exportExcel(key);
    }

    exportExcel = (selIndex) => {
        let  { pageIndex, pageSize, total, selectData, list,queryParam } = this.props;
        let param;
        if ( selIndex == '1') {
            if(selectData == 0) {
                Message.create({ content: '请先选择数据', color: 'warning', duration: 10 });
                return;
            }
            let ids = selectData.map((n) => n.id).join();
            param = {
                GET_TOEXPORTEXCEL:`/occ-fee/fee/activity-plan-template-excel/downloadMergeExcelTemplate`,
                dataList:{
                    ids:ids,
                    isEdit: selIndex == '1'?false:true,
                    isAll: false,
                    isPdf:false,
                    class:"com.yonyou.occ.fee.service.dto.ActivityPlanDto",
                    excelFileName: "活动申请模板"
                }
            }
            actions.ActivityPlanTemplate.exportExcel(param);
        } else if(selIndex == '2'){
            if(selectData == 0) {
                Message.create({ content: '请先选择数据', color: 'warning', duration: 10 });
                return;
            }
            let ids = selectData.map((n) => n.id).join();
            param = {
                GET_TOEXPORTEXCEL:`/occ-fee/fee/activity-plan-template-excel/downloadMergeExcelTemplate`,
                dataList:{
                    ids:ids,
                    isEdit: selIndex == '2'?false:true,
                    isAll: false,
                    isPdf:false,
                    class:"com.yonyou.occ.fee.service.dto.ActivityDesignDto",
                    excelFileName: "活动策划模板"
                }
            }
            actions.ActivityPlanTemplate.exportExcel(param);
        }

    }

    isEmpty = (data) => {
        if ( !data || (data && data.length === 0) ) {
            Warning('请选择数据');
            return true;
        }
        return false;
    }

    onFileChange =(info) => {
        if (info.file.status !== 'uploading') {
        }
        if (info.file.status === 'done') {
            let res = info.file.response;
            if (res.status) {
                Message.create({ content: res.msg, color: 'warning', duration: 10 });
                return;
            }
        } else if (info.file.status === 'error') {
            
        }
    }

    render() {
        let self = this;
        let { className,authCodeList} = self.props;
        //文件上传
        const fileUpload = {
            name: 'file',
            action: `/occ-fee/fee/activity-plan-excel/mergeActivityPlanTemplate-ExcelDataImport`,     //上传地址
            data: { isEdit: "false"},
            headers: {
            },
            accept: ".xlsx,.xls",
            showUploadList: false,
            beforeUpload: this.beforeUpload,
            onChange: this.onFileChange
        };
        //文件上传
        const fileUploadedit = {
            name: 'file',
            action: `/occ-fee/fee/activity-plan-excel/mergeActivityPlanTemplate-ExcelDataImport`,     //上传地址
            data: { isEdit: "true"},
            headers: {
            },
            accept: ".xlsx,.xls",
            showUploadList: false,
            beforeUpload: this.beforeUpload,
            onChange: this.onFileChange
        };


        const menu1 = (
            <Menu
                selectedKeys = {[]}
                onSelect={self.onSelect}>
                <Item key="1">导出活动方案单模板(新增)</Item>
                <Item key="2">导出活动策划单模板(新增)</Item>
            </Menu>
        );

        const menu2 = (
            <Menu>
                <Item key="0">
                    <Upload {...fileUpload}>
                        <span>导入新增数据</span>
                    </Upload>
                </Item>
                <Item key="1">
                    <Upload {...fileUploadedit}>
                        <span>导入修改数据</span>
                    </Upload>
                </Item>
            </Menu>
        );

        
        return (
            <div className="AcExport">
               {authCodeList.indexOf("ActivityPlanTemplateimport")!==-1 &&
                <Dropdown 
                   overlay={menu2}
                   animation="slide-up"
                 >
                    {/* <Button shape='border' style={{"marginLeft":15}} className={className} size='sm' >
                        导入
                    </Button> */}
                </Dropdown>
                }
                
                {
                // authCodeList.indexOf("ActivityPlanTemplateexport")!==-1 &&  
                <Dropdown
                   trigger={['click']}
                   overlay={menu1}
                   animation="slide-up"
                 >
                    <Button shape='border' style={{"marginLeft":15}} className={className} size='sm' >
                        导出
                    </Button>
                 </Dropdown>
               }

            </div>
        );
    }
}

export default AcExport;